package com.rfs.suanfa;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author renfushuai
 * @date 2022/2/10
 */
public class 存在重复元素 {
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> sets = new HashSet<>();
        for (int num : nums) {
            sets.add(num);
        }
        return sets.size() < nums.length;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{9,1,4,7,3,-1,0,5,8,-1,6};
        int i = longestConsecutive(nums);
        System.out.println(i);
    }

    public static int longestConsecutive(int[] nums) {
        if (nums.length==0){
            return 0;
        }
        int[] ints = IntStream.of(nums).sorted().distinct().toArray();
        int count = 1;
        int temp = 1;
        for (int i = 1; i < ints.length; i++) {
            if (ints[i] - ints[i - 1] == 1) {
                count++;
            } else {
                if (temp < count) {
                    temp = count;
                }
                count=1;
            }
        }
        return temp>count?temp:count;
    }
}
